package com.ccjq.shosestore.member.service.impl;

import com.ccjq.shosestore.core.utils.Md5Utils;
import com.ccjq.shosestore.core.utils.PojoUtils;
import com.ccjq.shosestore.core.utils.UuidUtils;
import com.ccjq.shosestore.member.bo.User;
import com.ccjq.shosestore.member.dao.UserDao;
import com.ccjq.shosestore.member.dao.impl.UserDaoImpl;
import com.ccjq.shosestore.member.po.UserModel;
import com.ccjq.shosestore.member.service.UserService;

public class UserServiceImpl implements UserService {
    private UserDao dao;
    public UserServiceImpl(){
        dao = new UserDaoImpl();
    }
    public boolean existx(String username) {
        UserModel model =dao.findUserByName(username);
        if (model == null)
            return false;
        else
            return true;
    }

    public String saveUser(User user) {
        String userId = UuidUtils.generateShortUUID("U",10);
        user.setUserId(userId);
        user.setPassword(Md5Utils.encoderByMd5(user.getPassword()));
        int result = dao.save(PojoUtils.bo2po(user,UserModel.class));
        return userId;
    }

    public User findUserById(String userId) {
        UserModel model =dao.findUserById(userId);
       return PojoUtils.po2bo(model,User.class);

    }

    public User findUserByNamePw(String username, String password) {
        Md5Utils.encoderByMd5(password);
        UserModel model= dao.findUserByNamePw(username,password);
        return PojoUtils.po2bo(model,User.class);
    }
}
